;; This is an operating system configuration template
;; for a "desktop" setup with GNOME and Xfce where the
;; root partition is encrypted with LUKS, and a swap file.

(define-module (guix-non-free)
 #:use-module (gnu)
 #:use-module (gnu packages linux)
 #:use-module (guix download)
 #:use-module (guix git-download)
 #:use-module (guix packages)
 #:use-module (guix licenses)
 #:use-module (guix build-system gnu)
 #:use-module (nongnu packages nvidia)
 #:use-module (nongnu services nvidia))

(define-public linux-non-free
 (package
  (inherit linux-libre)
  (name "linux-non-free")
  (version "6.16.9")
  (source
   (origin
    (method url-fetch)
    (uri "https://mirrors.tuna.tsinghua.edu.cn/kernel/v6.x/linux-6.16.9.tar.xz")
    (sha256
     (base32
      "13g59jvc6kvp8dzl6ysmzrpq4nh9xvy5va5avrsn6iq5ryiwij3s"))))
   (synopsis "A non-free Linux kernel allowing to use non-free firmware in GNU/Guix.")
   (description "A non-free Linux kernel from upstream.")
   (license gpl2)
   (home-page "https://kernel.org")))

(define-public firmware-non-free
 (package
  (name "firmware-non-free")
  (version "20250808")
  (source
   (origin
    (method url-fetch)
    (uri "https://mirrors.tuna.tsinghua.edu.cn/kernel/firmware/linux-firmware-20250808.tar.xz")
    (sha256
     (base32
      "0zw3vsmd07yr27y5fz0m357hci00ah5impx5sz4jcnd18ldmaaf0"))))
    (build-system gnu-build-system)
    (arguments
      (list #:make-flags #~(list (string-append "DESTDIR=" (assoc-ref %outputs "out")))
            #:phases #~(modify-phases %standard-phases
                      (delete 'configure)
                      (delete 'build)
                      (delete 'check)
                      (delete 'strip)
                      (delete 'validate-runpath))))
    (synopsis "Collection of non-free firmware for Linux kernel.")
    (description "Non free firmware collection.")
    (license (non-copyleft "various"))
    (home-page "https://mirrors.tuna.tsinghua.edu.cn/kernel/firmware/")))

(use-modules (guix-non-free) (gnu system nss) (guix utils))
(use-service-modules desktop sddm xorg ssh)
(use-package-modules certs gnome ssh)

(operating-system
  (host-name "Guix")
  (timezone "Asia/Shanghai")
  (locale "zh_CN.utf8")

  ;; Choose US English keyboard layout.  The "altgr-intl"
  ;; variant provides dead keys for accented characters.
  (keyboard-layout (keyboard-layout "us" "altgr-intl"))

  ;; Use the UEFI variant of GRUB with the EFI System
  ;; Partition mounted on /boot/efi.
  (bootloader (bootloader-configuration
                (bootloader grub-efi-bootloader)
                (targets '("/boot/efi"))
                (keyboard-layout keyboard-layout)))

  ;; Specify a mapped device for the encrypted root partition.
  ;; The UUID is that returned by 'cryptsetup luksUUID'.
  (file-systems (append
                 (list (file-system
                         (device (file-system-label "guix-root"))
                         (mount-point "/")
                         (type "ext4"))
                       (file-system
                         (device (uuid "E176-7DEA" 'fat))
                         (mount-point "/boot/efi")
                         (type "vfat")))
                 %base-file-systems))

  ;; Specify a swap file for the system, which resides on the
  ;; root file system.
  ;; (swap-devices (list (swap-space
  ;;                  (target "/swapfile"))))

  (kernel linux-non-free)
  (kernel-arguments (append
		     '("modprobe.blacklist=nouveau rootdelay=10")
		     %default-kernel-arguments))
  (firmware
   (append
     (list firmware-non-free)
     %base-firmware))

  (users (cons (user-account
                (name "hitmoon")
                (comment "hitmoon")
                (group "hitmoon")
                (supplementary-groups '("wheel" "netdev"
                                        "audio" "video")))
               %base-user-accounts))

  ;; Add the `students' group
  (groups (cons* (user-group
                  (name "hitmoon"))
                 %base-groups))

  ;; This is where we specify system-wide packages.
  (packages (map replace-mesa (append (list nvidia-settings) %base-packages)))


  ;; Add GNOME and Xfce---we can choose at the log-in screen
  ;; by clicking the gear.  Use the "desktop" services, which
  ;; include the X11 log-in service, networking with
  ;; NetworkManager, and more.
  (services (if (target-x86-64?)
	        (let ((my-services
		  (modify-services %desktop-services
		  (delete gdm-service-type)
                  (guix-service-type
		   config => (guix-configuration
                    		(inherit config)
                    		(substitute-urls '("https://mirror.sjtu.edu.cn/guix/" "https://bordeaux.guix.gnu.org" "https://substitutes.nonguix.org")))))))


                  (append (list (service gnome-desktop-service-type)
			      (service xfce-desktop-service-type)
                              ;; sshd
                              (service openssh-service-type
				  (openssh-configuration
				    (permit-root-login #t)
				       ))
			      (service nvidia-service-type)
                              (service gdm-service-type
				(gdm-configuration
                                 (auto-suspend? #f)
                                 (wayland? #t)))
                              (set-xorg-configuration
                               (xorg-configuration
				(modules (cons nvda %default-xorg-modules))
				(drivers '("nvidia"))
                                (keyboard-layout keyboard-layout))))
                        my-services)
		)

                ;; FIXME: Since GDM depends on Rust (gdm -> gnome-shell -> gjs
                ;; -> mozjs -> rust) and Rust is currently unavailable on
                ;; non-x86_64 platforms, we use SDDM and Mate here instead of
                ;; GNOME and GDM.
                (append (list (service mate-desktop-service-type)
                              (service xfce-desktop-service-type)
                              (set-xorg-configuration
                               (xorg-configuration
                                (keyboard-layout keyboard-layout))
                               sddm-service-type))
                        %desktop-services)
				  ))

  ;; Allow resolution of '.local' host names with mDNS.
  (name-service-switch %mdns-host-lookup-nss))
